<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Sms;
use app\common\model\AssetsChange;
use app\common\model\BookCollect;
use app\common\model\BrowseLog;
use app\common\model\User as UserModel;
use think\Validate;

/**
 * 会员接口
 */
class User extends Api
{
    protected $noNeedLogin = ['editPwd'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }
    public function info(){
        $user = UserModel::field('id,nickname,email,mobile,avatar,level,exp,partner,gender,birthday,money,status,invite_code,parent')->find($this->auth->id);
        $this->success('请求成功',$user);
    }

    /**
     * 会员中心
     */
    public function index()
    {
        $this->success('', ['welcome' => $this->auth->nickname]);
    }



    /**
     * 修改会员个人信息
     *
     * @param string $avatar   头像地址
     * @param string $nickname 昵称
     * @param string $gender   性别
     */
    public function profile()
    {
        $user = $this->auth->getUser();
        $nickname = $this->request->request('nickname','');
        $gender = $this->request->request('gender');
        $avatar = $this->request->request('avatar', '');

        if(!empty($nickname)){
            $user->nickname = $nickname;
        }
        if(!empty($gender) || $gender === '0'){
            $user->gender = $gender;
        }
        if(!empty($avatar)){
            $user->avatar = $avatar;
        }
        if($nickname || ($gender || $gender === '0') || $avatar){
            $user->save();
        }
        $this->success('修改成功');
    }


    public function assetsList(){
        $page = request()->post('page',1);
        $limit = request()->post('limit',config('paginate.list_rows'));
        $list = AssetsChange::where('coin_id','money')->where('user_id',$this->auth->id)->page($page,$limit)->field('id,user_id,amount,remark,status,created_at')->select();
        $this->success('请求成功',$list);
    }

    public function editPwd(){
        $post = request()->post();
        if(!isset($post['type']) || empty($post['type']) || !in_array($post['type'],[1,2])){
            $this->error('参数错误');
        }
        $rule = [
            'code'      => 'require',
            'mobile'    => 'require'
        ];
        if($post['type'] == 1){
            $rule['password'] = 'require';
            $event = 'editLoginPwd';
            $field = 'password';
        }else{
            $rule['password'] = 'require';
            $event = 'editPayPwd';
            $field = 'pay_password';
        }
        $validate = new Validate($rule,[],[
            'password'  =>'密码',
            'code'      =>'验证码',
            'mobile'    =>'手机号',
        ]);
        if (!$validate->check($post)) {
            $this->error($validate->getError());
        }
        $ret = Sms::check($post['mobile'], $post['code'], $event);
        if (!$ret) {
            $this->error(__('Captcha is incorrect'));
        }
        $sale = UserModel::where('mobile',$post['mobile'])->value('salt');
        $password = $this->auth->getEncryptPassword($post['password'],$sale);
        if($this->auth->id){
            $userId = $this->auth->id;
        }else{
            $userId = UserModel::where('mobile',$post['mobile'])->value('id');
        }
        $result = UserModel::update([$field=>$password],['id'=>$userId]);
        if($result === false){
            $this->error('修改失败，请稍后重试');
        }
        $this->success('修改成功');
    }
}
